package console.ant;

import collector.Configuration;
import console.App;
import console.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

/**
 * This class is the taskdef for invoking from within Ant.   For example:
 * <code>
 * <taskdef name="same" classname="console.ant.Same" classpath="${dir.dist}/same-${build.number}.jar"/>
 * <same src="src" minlengthforchunk="5" quiet="true" anonymizetypes="true"/>
 * </code>
 *
 * @author brianegge
 * @license http://www.freebsd.org/copyright/license.html  BSD License (4 Clause)
 */
@SuppressWarnings({"UnusedDeclaration"})
public class Same extends Task implements Logger {

    private final Configuration configuration = new Configuration();

    public void setAnonymizeTypes(boolean value) {
        configuration.setAnonymizeTypes(value);
    }

    public void setAnonymizeVariables(boolean value) {
        configuration.setAnonymizeVariables(value);
    }

    public void setAnonymizeLiterals(boolean value) {
        configuration.setAnonymizeLiterals(value);
    }

    public void setMinLengthForChunk(int length) {
        configuration.setMinLengthForChunk(length);
    }

    private boolean failOnError = true;

    public void setFailOnError(final boolean failOnError) {
        this.failOnError = failOnError;
    }

    public void setSrc(final String src) {
        this.src = src;
    }

    private String src;

    public void setQuiet(final boolean quiet) {
        this.quiet = quiet;
    }

    private boolean quiet;

    public void execute() {
        configuration.setLogger(this);
        try {
        	App app = new App(configuration, new String[]{src});
            int i = app.run();
            if (i != 0 && failOnError) {
                throw new BuildException("Duplicate code found");
            }
        } catch (Throwable e) {
        	e.printStackTrace();
            throw new BuildException(e);
        }
    }

    public void info(String s) {
        if (!quiet) {
            log(s);
        }
    }

    public void error(String s) {
        log(s, Project.MSG_ERR);
    }
}
